#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# This program is free software; you can redistribute it and/or modify  
# it under the terms of the GNU General Public License as published by  
# the Free Software Foundation; either version 2 of the License, or     
# (at your option) any later version.                                   
#                                                                         
# A copy of the license can be found in the license.txt file supplied   
# with this software or at: http://www.gnu.org/copyleft/gpl.html       
#


class   Memtable(object) :
    def __init__(self, table, column_family_name) :
        self.table = table
        self.column_family_name = column_family_name

        # map: row_key --> cf_object
        self._column_families = {}

        # Threshold in size 
        self._threshold_size = 64 * 1024 * 1024
        # threshold in object count
        self._threshold_count = 4 * 1024 * 1024

        # current size
        self._current_size = 0
        # current object count
        self._current_count = 0
        ## __init__()

    def put(self, row_key, column_family) :
        #assert self.column_family_name == column_family.name
        self.__resolve(row_key, column_family)
        ## put()

    def __resolve(self, row_key, column_family) :
        if row_key in self._column_families :
            old_cf = self._column_families[ row_key ]

            old_size = old_cf.getColumnSize()
            old_count = old_cf.getColumnCount()

            old_cf.addColumnFamily( column_family )

            new_size = old_cf.getColumnSize()
            new_count = old_cf.getColumnCount()

            self._current_count += new_count - old_count
            self._current_size += new_size - old_size

            local_delete_time = max(old_cf.getLocalDeletionTime(), column_family.getLocalDeletionTime())
            mark_for_delete_at = max(old_cf.getMarkedForDeleteAt(), column_family.getMarkedForDeleteAt())
            old_cf.markForDeleteAt(local_delete_time, mark_for_delete_at)
        else :
            self._column_families[ row_key ] = column_family
            self._current_count += column_family.getColumnCount()
            self._current_size += column_family.getColumnSize() + len(row_key)
        ## __resolve()

    def flush(self, commit_log_context) :
        raise NotImplementedError
        ## flush()

    def isThresholdViolated(self) :
        return (self._current_size >= self._threshold_size) or (self._current_count >= self._threshold_count)
        ## isThresholdViolated()

    # get column-family by row-key
    def __get(self, row_key) :
        if row_key in self._column_families :
            return self._column_families[ row_key ]

        return None
        ## __get()

    #### --------------------------------------------------------- ####

    def getNamesIterator(self, names_query_filter) :
        #assert names_query_filter.row_key is not None
        #assert len(names_query_filter.column_names) > 0
        #assert names_query_filter.getColumnFamilyName() is not None
        if not names_query_filter.row_key :
            raise RuntimeError,'Invalid row-key in NamesQueryFilter'
        cf = self.__get(names_query_filter.row_key)
            
        class   NamesIterator(object) :
            def __init__(self) :
                self._iter = iter(names_query_filter.column_names)
                ## __init__()

            def getColumnFamily(self) :
                column_family = cf
                if not column_family :
                    column_family_name = names_query_filter.getColumnFamilyName()
                    if not column_family_name :
                        raise RuntimeError,'Invalid Column-Family name in NamesQueryFilter'
                    column_family = ColumnFamily( column_family_name ) 

                return column_family
                ## getColumnFamily()

            def __iter__(self) :
                return  self
                ## __iter__()

            def next(self) :
                '''return IColumn
                '''
                if not cf :
                    raise StopIteration

                while True :
                    cname = self._iter.next()
                    column = cf.getColumn(cname)
                    if column :
                        return column
                raise StopIteration
                ## next()
            ## class NamesIterator

        return NamesIterator()
        ## getNamesIterator

    ## class Memtable

